package com.canbot.u05.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.bean.StandbyVoice;

import java.util.List;

public class RecordAdapter extends BaseAdapter {

        private Context mContext;

        private List<StandbyVoice> mVoices;

        private OnDeleteListener mDeleteListener;

        private OnPlayListener mPlayListener;

        public RecordAdapter(Context context, List<StandbyVoice> mVoices) {
                this.mContext = context;
                this.mVoices = mVoices;
        }

        @Override
        public int getCount() {
                return mVoices.size();
        }

        @Override
        public Object getItem(int position) {
                return mVoices.get(position);
        }

        @Override
        public long getItemId(int position) {
                return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
                final ItemView holder;
                if (convertView == null) {
                        convertView = LayoutInflater.from(mContext).inflate(R.layout.standby_item_records, null);
                        holder = new ItemView();
                        holder.text = (TextView) convertView.findViewById(R.id.tvVoice);
                        holder.ivPlay = (ImageView) convertView.findViewById(R.id.ivPlay);
                        holder.ivDelete = (ImageView) convertView.findViewById(R.id.ivDelete);

                        convertView.setTag(holder);
                }
                else {
                        holder = (ItemView) convertView.getTag();
                }

                holder.ivDelete.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                                if (mDeleteListener != null) {
                                        mDeleteListener.onDelete(holder.ivDelete, position);
                                }

                        }
                });
                holder.ivPlay.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                                if (mPlayListener != null) {
                                        mPlayListener.onPlay(holder.ivPlay, position);
                                }

                        }
                });
                holder.text.setText(mVoices.get(position).getContent());
                return convertView;
        }

        public void SetOnPlayListener(OnPlayListener playListener) {
                this.mPlayListener = playListener;
        }

        public void SetOnDeleteListener(OnDeleteListener deleteListener) {
                this.mDeleteListener = deleteListener;

        }

        class ItemView {

                private TextView text;

                private ImageView ivPlay;

                private ImageView ivDelete;
        }


        public interface OnPlayListener {

                void onPlay(ImageView ivPlay, int position);
        }

        public interface OnDeleteListener {

                void onDelete(ImageView ivDelete, int position);
        }
}
